package com.lagou.jdbc03;

import com.lagou.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @Author wsl
 * @Date 2021/8/19 21:40
 * @Project_Name mysql
 */
public class TestLogin01 {

    /**
     * 用户登录案例
     *使用Statement字符串拼接的方式完成查询
     * @param args
     */

    public static void main(String[] args) throws SQLException {

        //1.注册驱动
        Connection con = JDBCUtils.getConnection();

        //2.获取statement对象
        Statement statement = con.createStatement();

        //3.获取用户输入的用户名和密码
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = sc.nextLine();
        System.out.println("请输入密码：");
        String pass = sc.nextLine();

        //4.拼接SQL,执行查询
        String sql = "select * from jdbc_user " +
                "where username = " + " '" + name +"' " +" and password = " +" '" + pass +"'";
        System.out.println(sql);
        ResultSet resultSet = statement.executeQuery(sql);

        //5.处理结果集，判断结果是否为空
        if (resultSet.next()){
            System.out.println("登录成功！欢迎您："+name);
        } else {
            System.out.println("登录失败！");
        }

        //6.释放资源
        JDBCUtils.close(con,statement,resultSet);
    }
}
